/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package object;

import object.Sprite;
import utilities.ImageLoader;
import utilities.TileManager;
import core.PlayState;
import java.awt.Rectangle;

/**
 *Class containing the bullet enemy's information
 * 
 */

public final class BulletEnemy extends Sprite
{
    private static final int STEP=-10;
    private static final int STEP_OFFSET=2;

    private Player player;
    private PlayState playstate;

    //Creates a new BulletEnemy
    public BulletEnemy(int width,int height,ImageLoader imLd,Player play,PlayState ps)
    {
        super(width,height/2,width,height,imLd,"bullet");
        init();
        player=play;
        playstate=ps;
    }

    private void init()
    {
        int h=getPHeight()/2 + ((int)(getPHeight()*Math.random())/2);
        if (h+getHeight()>getPHeight())
            h -= getHeight();

        setPosition(getPWidth(),h);
        setStep(STEP+getRandRange(STEP_OFFSET),0);
    }

    private int getRandRange(int x)
    {
        return ((int)(2*x*Math.random()))-x;
    }
    
    public void hit()
    {

    }

    private void goneOffScreen()
    {
        if (((locx+getWidth())<=-1000)&&(dx<0))
        {
            init();
        }
    }

    private void hitPlayer()
    {
        Rectangle playerBox=player.getRectangle();
        playerBox.grow(-playerBox.width/3,0);

        if (playerBox.intersects(getRectangle()))
        {
            playstate.enemyHitPlayer(locx,locy+getHeight()/2);
            init();
        }
  }

    public void updateSprite()
    {
        hitPlayer();
        goneOffScreen();
        super.updateSprite();
    }
}

